//
//  LSIntercontinentalMatchVController.m
//  LookScore
//
//  Created by wr on 2018/1/31.
//  Copyright © 2018年 ChongQingXunTiCompany. All rights reserved.
//

#import "LSIntercontinentalMatchVController.h"
#import "UIViewController+ZJScrollPageController.h"
#import "ZJScrollPageView.h"
#import "CustomSegmentedView.h"
#import "LSIntercontinentalModel.h"
#import "LSIntercontinentalMatchCell.h"
#import "LSLeagueCompetitionBaseVController.h"//联赛赛事
@interface LSIntercontinentalMatchVController ()<UITableViewDelegate,UITableViewDataSource,ZJScrollPageViewDelegate>

@property(strong,nonatomic)UITableView *tableView;

@property(strong,nonatomic)NSMutableArray *modelArray;

@property(strong,nonatomic)NSArray *titlesArray;

@property(strong,nonatomic)NSMutableArray *dataSource;

@property(nonatomic,strong)NSMutableArray*   isOpenArr;

@end

@implementation LSIntercontinentalMatchVController{
    NSInteger scrollIndex;
    NSInteger segmentIndex;
}

-(UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc]init];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor =LSBackGroundColor;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return _tableView;
}
-(NSMutableArray *)modelArray{
    if(!_modelArray){
        _modelArray=[[NSMutableArray alloc]init];
    }
    return _modelArray;
}

- (NSMutableArray *)dataSource{
    if (!_dataSource) {
        _dataSource = [NSMutableArray array];
    }
    return _dataSource;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.isOpenArr=[[NSMutableArray alloc] init];
    segmentIndex = 0;
}

-(void)zj_viewDidLoadForIndex:(NSInteger)index{
    scrollIndex = index;
    if (index>0) {
        self.titlesArray=@[@"洲际赛事",@"国家赛事"];
        if (index==6) {
            self.titlesArray=@[@"国际赛事",@"沙滩赛事"];
        }
        self.tableView.tableHeaderView=[self tableHeadView];
    }
    [self dataClassification];
    [self.view addSubview:self.tableView];
     self.tableView.sd_layout.leftEqualToView(self.view).topEqualToView(self.view).rightEqualToView(self.view).bottomEqualToView(self.view);
}
- (void)dataClassification{
    NSArray *classesArr = @[@"",@"欧洲赛事",@"亚洲赛事",@"美洲赛事",@"非洲赛事",@"大洋洲赛事",@"国际赛事"];
    [self.dataSource removeAllObjects];
    if (scrollIndex == 0) {
        self.dataSource = self.leagueListArr.copy;
    }
    
    else{
        if (segmentIndex == 0) {
            for (NSDictionary *dic in self.leagueListArr) {
                if ([dic[@"LeagueBelongsName"] isEqualToString:classesArr[scrollIndex]]) {
                    [self.dataSource addObject:dic];
                }
            }
            
        }
        if (segmentIndex == 1) {
            NSMutableArray *dataSource = [NSMutableArray array];
            for (NSDictionary *dic in self.leagueListArr) {
                if (![dic[@"LeagueBelongsName"] isEqualToString:classesArr[scrollIndex]]) {
                    [dataSource addObject:dic];
                    NSMutableSet * mSet = [NSMutableSet set];
                    for (NSDictionary * dic in dataSource) {//添加所有LeagueBelongsName的值
                        [mSet addObject:dic[@"LeagueBelongsName"]];
                    }
                    NSArray * allQaType = [mSet allObjects];
                    NSMutableArray * allMArr = [NSMutableArray array];//存入所有相同元素数组
                    for (NSString * str1 in allQaType) {
                        
                        NSMutableDictionary *firstDic = [NSMutableDictionary dictionary];
                        NSMutableArray *sameNameLeague = [NSMutableArray array];
                         for (NSDictionary * dic in dataSource) {
                             if ([dic[@"LeagueBelongsName"] isEqualToString:str1]) {
                                 [sameNameLeague addObject:dic];
                                 
                                 [firstDic setObject:str1 forKey:@"LeagueName"];
                                 [firstDic setObject:sameNameLeague forKey:@"LeagueList"];
                                 [firstDic setObject:dic[@"PictureName"] forKey:@"ContryImage"];
                             }
                         }
                        [allMArr addObject:firstDic];
                    }
                    self.dataSource = allMArr;
                }
            }
        }
        MyLog(@"dataSource --- %@",self.dataSource);
    }
    
    for (int i=0; i<self.dataSource.count; i++) {
        NSString*  state=@"close";
        [self.isOpenArr addObject:state];
    }
    
    [self.tableView reloadData];
    
    
}
#pragma mark 🔥 tableViewDelegate tableViewDataSource
-(UIView *)tableHeadView{
 
    UIView *backView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, LSScreenWidth, 44)];
    backView.backgroundColor=LSTitleWhiteColor;
    
    CustomSegmentedView *aCustomSegmentedView=[[CustomSegmentedView alloc]initWidthCustomSegmentedViewFrame:CGRectMake((LSScreenWidth-180)/2, 10, 180, 24) titlesArray:self.titlesArray seletedTitleColor:[UIColor whiteColor] normalTitleColor:LSSubColorFF9E44 seletedBgColor:LSSubColorFF9E44 normalBgColor:[UIColor whiteColor] bordeColor:LSSubColorFF9E44  borderWidth:1 seletedCustomViewIndex:^(NSInteger index) {
        segmentIndex = index;
        [self dataClassification];
        
    }];
    [aCustomSegmentedView selectedDefaultSegmentedIndex:0];
    [backView addSubview:aCustomSegmentedView];
    
    
    UIView *lineView=[[UIView alloc]initWithFrame:CGRectMake(0, 44, LSScreenWidth,.5f)];
    lineView.backgroundColor=LSLineGrayColor;
    [backView addSubview:lineView];
    
    return backView;
    
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
        return self.dataSource.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    NSString*  state=[self.isOpenArr objectAtIndex:section];
    if ([state isEqualToString:@"open"]) {
        self.modelArray = self.dataSource[section][@"LeagueList"];
        return self.modelArray.count;
    }
    return 0;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, LSScreenWidth, 35)];
    
    UIImageView *imageView = [[UIImageView alloc] init];
    
    
//    [self.guestTeamLogo sd_setImageWithURL:[NSURL URLWithString:TeamImageHeaderUrl((long)hotMatchModel.HomeTeamId)] placeholderImage:[UIImage imageNamed:@"logoNull"] options:SDWebImageRefreshCached];
    [imageView sd_setImageWithURL:[NSURL URLWithString:InfoLeagueImageHeaderUrl(self.dataSource[section][@"LeagueImage"])] placeholderImage:[UIImage imageNamed:@"logoNull"] options:SDWebImageRefreshCached];
//    imageView.image = [UIImage imageNamed:self.dataSource[section][@"LeagueImage"]];
    [header addSubview:imageView];
    imageView.sd_layout.leftEqualToView(header).offset(14).topEqualToView(header).offset(7).heightIs(30).widthIs(30);
    
    UIView *lineView = [[UIView alloc] init];
    lineView.backgroundColor = LSLineGrayColor;
    [header addSubview:lineView];
    lineView.sd_layout.leftEqualToView(header).rightEqualToView(header).bottomEqualToView(header).offset(-1).heightIs(1);
    
    UILabel *leagueNameLab = [[UILabel alloc] init];
    leagueNameLab.font = LSFontSize13;
    leagueNameLab.text = self.dataSource[section][@"LeagueName"];
    [header addSubview:leagueNameLab];
    leagueNameLab.sd_layout.leftSpaceToView(imageView, 14).topEqualToView(header).offset(7).widthIs(200).heightIs(25);
    
    
    UIImageView *arrowImageView = [[UIImageView alloc] init];
    arrowImageView.image = [UIImage imageNamed:@"MineRightLightGray"];
    [header addSubview:arrowImageView];
    arrowImageView.sd_layout.rightEqualToView(header).offset(-24).topEqualToView(header).offset(10).widthIs(10).heightIs(15);
    
    UIButton*  button=[UIButton buttonWithType:UIButtonTypeCustom];
    button.tag=100+section;
    [button addTarget:self action:@selector(ClickSection:) forControlEvents:UIControlEventTouchUpInside];
    [header addSubview:button];
    button.sd_layout.leftEqualToView(header).rightEqualToView(header).topEqualToView(header).bottomEqualToView(header);
    
//    if (segmentIndex == 0) {
//        imageView.image = [UIImage imageNamed:self.firstLevelList[section][@"LeagueImage"]];
//        leagueNameLab.text = self.firstLevelList[section][@"LeagueName"];
//    }
//    if (segmentIndex == 1) {
//        imageView.image = [UIImage imageNamed:self.secondLevelList[section][@"LeagueImage"]];
//
//        leagueNameLab.text = self.secondLevelList[section][@"LeagueName"];
//    }
    
    return header;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return  35;
}



- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
  
    LSIntercontinentalMatchCell *cell=[LSIntercontinentalMatchCell IntercontinentalMatchCellByTableView:tableView];
    
    
    LSIntercontinentalModel *model = [LSIntercontinentalModel mj_objectWithKeyValues:self.modelArray[indexPath.row]];
    cell.model = model;
    return cell;
}


-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 44;
}

-(void)ClickSection:(UIButton*)sender
{
    if (segmentIndex == 0) {
        LSLeagueCompetitionBaseVController *baseVC=[[LSLeagueCompetitionBaseVController alloc]init];
        baseVC.leagueInfoDic = self.dataSource[sender.tag-100];
        [self.navigationController pushViewController:baseVC animated:YES];
    }
    else{
        NSString*  state=[self.isOpenArr objectAtIndex:sender.tag-100];
        if ([state isEqualToString:@"open"]) {
            state=@"close";
        }else
        {
            state=@"open";
        }
        self.isOpenArr[sender.tag-100]=state;
        NSIndexSet *indexSet = [[NSIndexSet alloc] initWithIndex:sender.tag-100];
        [self.tableView reloadSections:indexSet withRowAnimation:UITableViewRowAnimationFade];
    }
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    LSLeagueCompetitionBaseVController *baseVC=[[LSLeagueCompetitionBaseVController alloc]init];
    baseVC.leagueInfoDic = self.modelArray[indexPath.row];
    [self.navigationController pushViewController:baseVC animated:YES];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
